package com.qf.controller;

import com.qf.dto.R;
import com.qf.entity.SysCategory;
import com.qf.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author ztf
 * @Date 17:43 2021/12/29
 * 功能：添加、修改、根据id删除、根据分类名查找、根据id查找、查询所有
 **/
@RestController
@RequestMapping("category")
public class CategoryController {
    private CategoryService categoryService;

    @Autowired
    public void setCategoryService(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    //=================查==================

    /**
     * 查询所有分类
     *
     * @return 所有分类
     */
    @GetMapping("/categories")
    public R categories() {
        List<SysCategory> categoryList = categoryService.findAllCategory();
        return R.setOK(categoryList);
    }

    /**
     * 根据id查询分类
     *
     * @param id id
     * @return 分类
     */
    @GetMapping("categorybyid/{id}")
    public R categoryById( @PathVariable Integer id) {
        SysCategory category = categoryService.findCategoryById(id);
        return R.setOK(category);
    }

    /**
     * 根据分类名查询
     *
     * @param cname 分类名
     * @return 分类集合
     */
    @GetMapping("categoryByName")
    public R categoryByName(String cname) {
        List<SysCategory> categoryList = categoryService.findCategoryByName(cname);
        return R.setOK(categoryList);
    }

    /**
     * 添加分类
     * @param sysCategory 分类
     * @return OK
     */
    @PostMapping("/category")
    public R addCategory(@RequestBody SysCategory sysCategory){
        categoryService.addCategory(sysCategory);
        return R.setOK();
    }

    /**
     * 根据id删除分类
     *
     * @param ids id
     * @return
     */
    @DeleteMapping("deletecategorybyid")
    public R deleteCategoryByIds(@RequestBody List<Integer> ids) {
        Integer count = categoryService.deleteCategoryByIds(ids);
        return R.setOK();
    }

}
